define([], function () {
    require([], function () {
    //绑定data-toggle=addresspicker属性点击事件

    $(document).on('click', "[data-toggle='addresspicker']", function () {
        var that = this;
        var callback = $(that).data('callback');
        var input_id = $(that).data("input-id") ? $(that).data("input-id") : "";
        var lat_id = $(that).data("lat-id") ? $(that).data("lat-id") : "";
        var lng_id = $(that).data("lng-id") ? $(that).data("lng-id") : "";
        var lat = lat_id ? $("#" + lat_id).val() : '';
        var lng = lng_id ? $("#" + lng_id).val() : '';
        var url = "/addons/address/index/select";
        url += (lat && lng) ? '?lat=' + lat + '&lng=' + lng : '';
        Fast.api.open(url, '位置选择', {
            callback: function (res) {
                input_id && $("#" + input_id).val(res.address).trigger("change");
                lat_id && $("#" + lat_id).val(res.lat).trigger("change");
                lng_id && $("#" + lng_id).val(res.lng).trigger("change");
                try {
                    //执行回调函数
                    if (typeof callback === 'function') {
                        callback.call(that, res);
                    }
                } catch (e) {

                }
            }
        });
    });
});

require.config({
    paths: { 'watermark-dom': '../addons/pagemark/js/watermark', }
});

require(['watermark-dom'], function (watermark) {
    var config = Config.pagemark.config;
    var userinfo = Config.pagemark.userinfo;
    if (config.mark_area == undefined || config.mark_area.value.indexOf('close') != -1) {
        return;
    }
    // 后台
    if (config.mark_area.value.indexOf('backend') != -1) {
        let setting = getWaterMarkSetting(config, userinfo);
        if (Config.controllername == 'index' && Config.actionname == 'index') {
            markWorking(watermark, setting, config);
            return;
        }
        // 无菜单后台页面
        if (Config.modulename == 'admin') {
            let url = new URL(window.location.href);
            if (url.searchParams.get('ref') != 'addtabs' && url.searchParams.get('addtabs') != '1') {
                if (Config.actionname != 'login') {
                    markWorking(watermark, setting, config);
                }
                return;
            }
        }
    }
    // user
    if (config.mark_area.value.indexOf('user') != -1 && Config.controllername == 'user') {
        if (Config.actionname != 'login' && Config.actionname != 'register') {
            let setting = getWaterMarkSetting(config, userinfo);
            markWorking(watermark, setting, config);
        }
    }

    function markWorking(wm, setting, config) {
        wm.init(setting);
        // 是否开启动态水印
        if (config.dmc_mark != undefined && config.dmc_mark['value'] == '1') {
            // 时间间隔
            var dmc_time = (config.dmc_time != undefined && config.dmc_time['value'] > 0) ? Number(config.dmc_time['value']) : 5;
            setInterval(function () {
                // 是否开启时间戳水印
                var txt = (config.timestamp_mark != undefined && config.timestamp_mark['value'] == '1') ? (setting.watermark_txt + ' ' + getFormatDateNow()) : setting.watermark_txt;
                wm.load({ watermark_txt: txt });
            }, 1000 * dmc_time);
        }
    }
    function getWaterMarkSetting(config, userinfo) {
        var watermarksetting = {
            watermark_id: 'wm_div_id',          //水印总体的id
            watermark_prefix: 'mask_div_id',    //小水印的id前缀
            watermark_txt: "",      		//水印的内容
            watermark_x: 20,                     //水印起始位置x轴坐标
            watermark_y: 20,                     //水印起始位置Y轴坐标
            watermark_rows: 0,                   //水印行数
            watermark_cols: 0,                   //水印列数
            watermark_x_space: 50,              //水印x轴间隔
            watermark_y_space: 50,               //水印y轴间隔
            watermark_font: '微软雅黑',           //水印字体
            watermark_color: 'black',            //水印字体颜色
            watermark_fontsize: '17px',          //水印字体大小
            watermark_alpha: 0.16,               //水印透明度，要求设置在大于等于0.005
            watermark_width: 250,                //水印宽度
            watermark_height: 100,               //水印长度
            watermark_angle: 15,                 //水印倾斜度数
            watermark_parent_width: 0,      //水印的总体宽度（默认值：body的scrollWidth和clientWidth的较大值）
            watermark_parent_height: 0,     //水印的总体高度（默认值：body的scrollHeight和clientHeight的较大值）
            watermark_parent_node: null,     //水印插件挂载的父元素element,不输入则默认挂在body上
            monitor: true,                   //monitor 是否监控， true: 不可删除水印; false: 可删水印。
        };
        // 水印内容
        var watermark_txt = [];
        if (config.text.value.indexOf('username') != -1 && userinfo.username != undefined && userinfo.username.length > 0) {
            watermark_txt.push(userinfo.username);
        }
        if (config.text.value.indexOf('nickname') != -1 && userinfo.nickname != undefined && userinfo.nickname.length > 0) {
            watermark_txt.push(userinfo.nickname);
        }
        if (config.text.value.indexOf('email') != -1 && userinfo.email != undefined && userinfo.email.length > 0) {
            watermark_txt.push(userinfo.email);
        }
        if (config.text.value.indexOf('mobile') != -1 && userinfo.mobile != undefined && userinfo.mobile.length > 0) {
            watermark_txt.push(userinfo.mobile);
        }
        if (config.text.value.indexOf('append') != -1 && config.cstmd_text['value'].length > 0) {
            watermark_txt.push(config.cstmd_text['value']);
        }
        watermarksetting.watermark_txt = watermark_txt.join(" ");

        // 其他设置
        var list = ['x', 'y', 'rows', 'cols', 'x_space', 'y_space', 'color', 'font', 'width', 'height', 'angle', 'fontsize', 'alpha'];
        list.forEach(function (k) {
            if (config[k] != undefined && config[k]['value'].length > 0) {
                var settingKey = 'watermark_' + k;
                if (config[k]['type'] == 'number') {
                    config[k]['value'] = Number(config[k]['value']);
                }
                watermarksetting[settingKey] = config[k]['value'];
            }
        });
        return watermarksetting;
    }

    function getFormatDateNow() {
        var date = new Date();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        var hours = date.getHours();
        var minutes = date.getMinutes();
        var seconds = date.getSeconds();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        if (hours >= 0 && hours <= 9) {
            hours = "0" + hours;
        }
        if (minutes >= 0 && minutes <= 9) {
            minutes = "0" + minutes;
        }
        if (seconds >= 0 && seconds <= 9) {
            seconds = "0" + seconds;
        }
        return date.getFullYear() + "-" + month + "-" + strDate
            + " " + date.getHours() + ":" + minutes + ":" + seconds;
    }
});

require.config({
    paths: {
        'summernote': '../addons/summernote/lang/summernote-zh-CN.min'
    },
    shim: {
        'summernote': ['../addons/summernote/js/summernote.min', 'css!../addons/summernote/css/summernote.css'],
    }
});
require(['form', 'upload'], function (Form, Upload) {
    var _bindevent = Form.events.bindevent;
    Form.events.bindevent = function (form) {
        _bindevent.apply(this, [form]);
        try {
            //绑定summernote事件
            if ($(Config.summernote.classname || '.editor', form).size() > 0) {
                var selectUrl = typeof Config !== 'undefined' && Config.modulename === 'index' ? 'user/attachment' : 'general/attachment/select';
                require(['summernote'], function () {
                    var imageButton = function (context) {
                        var ui = $.summernote.ui;
                        var button = ui.button({
                            contents: '<i class="fa fa-file-image-o"/>',
                            tooltip: __('Choose'),
                            click: function () {
                                parent.Fast.api.open(selectUrl + "?element_id=&multiple=true&mimetype=image/", __('Choose'), {
                                    callback: function (data) {
                                        var urlArr = data.url.split(/\,/);
                                        $.each(urlArr, function () {
                                            var url = Fast.api.cdnurl(this, true);
                                            context.invoke('editor.insertImage', url);
                                        });
                                    }
                                });
                                return false;
                            }
                        });
                        return button.render();
                    };
                    var attachmentButton = function (context) {
                        var ui = $.summernote.ui;
                        var button = ui.button({
                            contents: '<i class="fa fa-file"/>',
                            tooltip: __('Choose'),
                            click: function () {
                                parent.Fast.api.open(selectUrl + "?element_id=&multiple=true&mimetype=*", __('Choose'), {
                                    callback: function (data) {
                                        var urlArr = data.url.split(/\,/);
                                        $.each(urlArr, function () {
                                            var url = Fast.api.cdnurl(this, true);
                                            var node = $("<a href='" + url + "'>" + url + "</a>");
                                            context.invoke('insertNode', node[0]);
                                        });
                                    }
                                });
                                return false;
                            }
                        });
                        return button.render();
                    };
                    $(Config.summernote.classname || '.editor', form).each(function () {
                        $(this).summernote($.extend(true, {}, {
                            height: 250,
                            lang: 'zh-CN',
                            fontNames: [
                                'Arial', 'Arial Black', 'Serif', 'Sans', 'Courier',
                                'Courier New', 'Comic Sans MS', 'Helvetica', 'Impact', 'Lucida Grande',
                                "Open Sans", "Hiragino Sans GB", "Microsoft YaHei",
                                '微软雅黑', '宋体', '黑体', '仿宋', '楷体', '幼圆',
                            ],
                            fontNamesIgnoreCheck: [
                                "Open Sans", "Microsoft YaHei",
                                '微软雅黑', '宋体', '黑体', '仿宋', '楷体', '幼圆'
                            ],
                            toolbar: [
                                ['style', ['style', 'undo', 'redo']],
                                ['font', ['bold', 'underline', 'strikethrough', 'clear']],
                                ['fontname', ['color', 'fontname', 'fontsize']],
                                ['para', ['ul', 'ol', 'paragraph', 'height']],
                                ['table', ['table', 'hr']],
                                ['insert', ['link', 'picture', 'video']],
                                ['select', ['image', 'attachment']],
                                ['view', ['fullscreen', 'codeview', 'help']],
                            ],
                            buttons: {
                                image: imageButton,
                                attachment: attachmentButton,
                            },
                            dialogsInBody: true,
                            followingToolbar: false,
                            callbacks: {
                                onChange: function (contents) {
                                    $(this).val(contents);
                                    $(this).trigger('change');
                                },
                                onInit: function () {
                                },
                                onImageUpload: function (files) {
                                    var that = this;
                                    //依次上传图片
                                    for (var i = 0; i < files.length; i++) {
                                        Upload.api.send(files[i], function (data) {
                                            var url = Fast.api.cdnurl(data.url, true);
                                            $(that).summernote("insertImage", url, 'filename');
                                        });
                                    }
                                }
                            }
                        }, $(this).data("summernote-options") || {}));
                    });
                });
            }
        } catch (e) {

        }

    };
});

});